package com.i2works.smartluolong.manage.controller.suggestion;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.manage.entity.admin.AdminOrgDO;
import com.i2works.smartluolong.manage.entity.admin.AdminOrgQuery;
import com.i2works.smartluolong.manage.service.admin.IAdminOrgService;
import com.i2works.smartluolong.manage.utils.BaseModule;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.entity.Tree;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.common.TreeUtil;
import com.i2works.smartluolong.web.entity.suggestion.SuggestionDO;
import com.i2works.smartluolong.web.entity.suggestion.SuggestionQuery;
import com.i2works.smartluolong.web.service.suggestion.ISuggestionService;

/**
 * 投诉及建议
 *
 * @author liu Yapeng
 * @date 2018/7/23 20:07
 */

@Controller
@RequestMapping("/suggestion")
public class SuggestionController extends BaseModule {
	
	private final ISuggestionService   suggestionService;

	private final IAdminOrgService adminOrgService;

	  public SuggestionController(ISuggestionService   suggestionService,IAdminOrgService adminOrgService) {
		  this.suggestionService=suggestionService;
		  this.adminOrgService=adminOrgService;
	  }

	    /**
	     * 组织机构页面
	     */
	    @GetMapping(value = "/view/index")
	    public String index() {
	        return "suggestion/index";
	    }


	    /**
	     * 无分页父类资源树
	     *
	     * @param showStatus：0显示所有的目录（不含按钮），1显示全部分类, 2显示所有正常父类
	     */
	    @RequestMapping(value = "/view/tree/{showStatus:^[0-2{1}]$}")
	    @ResponseBody
	    public List<Tree> queryTreeList(@PathVariable Integer showStatus) {
	        List<Tree> result = Lists.newArrayList();
	        try {
	            AdminOrgQuery query = new AdminOrgQuery();
	            query.setShowStatus(showStatus);
	            List<AdminOrgDO> resList = adminOrgService.queryTreeList(query);
	            List<Tree> treeDataList = Lists.newArrayList();
	            for (AdminOrgDO adminOrg : resList) {
	                if (null != this.orgTree(adminOrg)&&adminOrg.getStatus()==1) {
	                    treeDataList.add(this.orgTree(adminOrg));
	                }
	            }
	            result = TreeUtil.getNodeTree(0, treeDataList);
	        } catch (Exception e) {
	            logger.error("", e);
	        }
	        return result;
	    }
	    

	    /**
	     * 组织机构列表
	     */
	    @GetMapping(value = "/view/api")
	    @ResponseBody
	    public Result queryList(@ModelAttribute  SuggestionQuery   query) {
	        try {
	            int total = suggestionService.queryCount(query);
	            List<SuggestionDO> suggestionList = Lists.newArrayList();
	            if (total > 0) {
	            	suggestionList = suggestionService.queryList(query);
	            }
	            suggestionList= transform(suggestionList);
	            return R.success(suggestionList, total);
	        } catch (Exception e) {
	            logger.error("", e);
	            return R.error(MsgEnum.SYSTEM_ERROR.getMsg());
	        }
	    }

	    private  List<SuggestionDO>  transform(List<SuggestionDO>   list){
	    	List<SuggestionDO>   newList=new ArrayList<SuggestionDO>();
	    	AdminOrgQuery query=new AdminOrgQuery();
             if(list.size()!=0) {
            	 for (SuggestionDO suggestionDO : list) {
                    AdminOrgDO org=new AdminOrgDO();
					try {
						query.setId(suggestionDO.getOrgId());
						org = adminOrgService.query(query);
						suggestionDO.setOrgName(org.getOrgName());
						newList.add(suggestionDO);
					} catch (ServiceException e) {
						e.printStackTrace();
					}
				}
             }
	    	
	    	return newList;
	    }
	    /**
	     * 提交修改投诉建议
	     */
	    @PostMapping("/save")
	    @ResponseBody
	    public Result  save(SuggestionDO  suggestion) {
	    	try {
	    		suggestion.setCreateName(getUserName());
	    		suggestion.setUpdateName(getUserName());
	    	    suggestion.setUserId(getUserId());
				suggestionService.saveOrUpdate(suggestion);
			} catch (ServiceException e) {
				e.printStackTrace();
				 return R.error();
			}
	        return R.success();
	    }
    
	    
	    
	    /**
	     * 生成过滤后的树结构
	     */
	    private Tree orgTree(AdminOrgDO org) throws Exception {
	        Tree tree = new Tree();
	        tree.setId(org.getId());
	        tree.setPid(org.getPid());
	        if (org.getStatus() == 0) {
	            tree.setText(org.getOrgName() + "『禁』");
	        } else {
	            tree.setText(org.getOrgName());
	        }
	        tree.setType(org.getOrgType());
	        tree.setSort(org.getSort());
	        tree.setStatus(org.getStatus());
	        tree.setUrl(org.getOrgCode());
	        return tree;
	    }

  
}
